package com.example.gsp.mybatis.session;

import com.example.gsp.mybatis.binding.MapperdProxy;
import com.example.gsp.mybatis.config.Configuration;
import com.example.gsp.mybatis.config.MappedStatement;
import com.example.gsp.mybatis.excutor.DefaultExcutor;
import com.example.gsp.mybatis.excutor.Excutor;
import org.apache.ibatis.binding.MapperProxy;

import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.util.List;

public class DefaultSqlSession implements SqlSession {

    private Configuration conf;

    private Excutor excutor;

    public DefaultSqlSession(Configuration conf) {
        this.conf = conf;
        excutor = new DefaultExcutor(conf);
    }

    @Override
    public <T> T selectOne(String statement) {
        return null;
    }

    @Override
    public <T> T selectOne(String statement, Object parameter) {
        List<T> resultList = selectList(statement, parameter);
        if (resultList != null && resultList.size() == 1) {
            return resultList.get(0);
        } else {
            throw new RuntimeException("much result");
        }
    }

    @Override
    public <E> List<E> selectList(String statement) {
        return selectList(statement, null);
    }

    @Override
    public <E> List<E> selectList(String statement, Object parameter) {
        MappedStatement mappedStatement = conf.getMappedStatementMap().get(statement);
        return excutor.query(mappedStatement, parameter);
    }

    @Override
    public int insert(String statement) {
        return 0;
    }

    @Override
    public int insert(String statement, Object parameter) {
        return 0;
    }

    @Override
    public int update(String statement) {
        return 0;
    }

    @Override
    public int update(String statement, Object parameter) {
        return 0;
    }

    @Override
    public int delete(String statement) {
        return 0;
    }

    @Override
    public int delete(String statement, Object parameter) {
        return 0;
    }

    @Override
    public void commit() {

    }

    @Override
    public void commit(boolean force) {

    }

    @Override
    public void rollback() {

    }

    @Override
    public void rollback(boolean force) {

    }

    @Override
    public void close() {

    }

    @Override
    public void clearCache() {

    }

    @Override
    public Configuration getConfiguration() {
        return null;
    }

    @Override
    public <T> T getMapper(Class<T> type) {
        return (T) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{type}, new MapperdProxy(this));
    }

    @Override
    public Connection getConnection() {
        return null;
    }

}
